package com.zj.osm.view.mine.changedb

import android.content.Context
import android.os.Build
import android.view.View
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.flyco.dialog.listener.OnBtnClickL
import com.flyco.dialog.widget.NormalDialog
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder
import com.zj.osm.R
import com.zj.utils.view.PopupList
import java.io.File
import java.text.SimpleDateFormat
import java.util.*

/**
 * @project OsmDroid
 * @author jiang zhu
 * @data 2019/12/6
 */
class ChangeDBAdapter (var context: Context, layoutId: Int,var rowsBeanList: ArrayList<File>) :
        CommonAdapter<File>(context, layoutId, rowsBeanList) {

    override fun convert(holder: ViewHolder?, t: File?, position: Int) {
        val zipItem = holder?.getView<RelativeLayout>(R.id.zipItem)
        val icon = holder?.getView<ImageView>(R.id.icon)
        val name = holder?.getView<TextView>(R.id.name)
        val time = holder?.getView<TextView>(R.id.desc)
        val lineOnClick = holder?.getView<RelativeLayout>(R.id.lineOnClick)
        val date = t?.lastModified()
        time?.text = SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(Date(date!!))
        icon?.setImageResource(R.drawable.zip)
        //判断是否是当前数据库
        if(SPUtils.getInstance().getString("DATA_FILENAME","")==t.absolutePath) {
            lineOnClick?.visibility = View.VISIBLE
        }else{
            lineOnClick?.visibility = View.GONE
        }
        name?.text = t.name
        zipItem?.setOnClickListener {
            if(SPUtils.getInstance().getString("DATA_FILENAME","")==t.absolutePath) {
                ToastUtils.showShort("当前数据库切不能切换")
            }else{
                ///这里去切换数据库哈
                val dialog = NormalDialog(context)
                dialog.content("您确定要切换${t.name}为当前项目数据库么?")//
                        .style(NormalDialog.STYLE_TWO)//
                        .btnNum(2)
                        .title("切换数据库")
                        .btnText("取消", "切换数据库")//
                        .show()

                dialog.setOnBtnClickL(OnBtnClickL {
                    dialog.dismiss()
                }, OnBtnClickL {
                    dialog.dismiss()
                    SPUtils.getInstance().put("DATA_FILENAME", t.absolutePath)
                    SPUtils.getInstance().put("DATABASE_FILENAME","bhne"+System.currentTimeMillis().toString()+".db")
                    notifyDataSetChanged()
                    ToastUtils.showShort("数据库切换成功")
                })
            }

        }


        val popupList = PopupList(context)//长按弹出popupwindow
        popupList.bind(zipItem, listOf("路径", "切换数据库", "删除"), object : PopupList.PopupListListener {
            override fun showPopupList(adapterView: View, contextView: View, contextPosition: Int): Boolean {
                return true
            }

            @RequiresApi(Build.VERSION_CODES.N)
            override fun onPopupListClick(contextView: View, contextPosition: Int, position: Int) {
                /*Toast.makeText(this@ZipActivity, "$contextPosition,$position", Toast.LENGTH_SHORT).show()*/
                if (contextPosition == 0 && position == 2) {//删除
                    if(SPUtils.getInstance().getString("DATA_FILENAME","")==t.absolutePath) {
                        ToastUtils.showShort("当前数据库切不能切换")
                    }else{
                        val dialog = NormalDialog(context)
                        dialog.content("您确定要删除${t.name}吗?")//
                                .style(NormalDialog.STYLE_TWO)//
                                .btnNum(2)
                                .title("删除提示")
                                .btnText("取消", "删除")//
                                .show()
                        dialog.setOnBtnClickL(OnBtnClickL {
                            dialog.dismiss()
                        }, OnBtnClickL {
                            if (FileUtils.deleteFile(t.absolutePath)) {
                                ToastUtils.showShort("删除成功")
                                dialog.dismiss()
                                //删除文件
                                rowsBeanList.remove(t)
                                notifyDataSetChanged()
                            } else {
                                ToastUtils.showShort("删除失败")
                            }
                        })}
                } else if (contextPosition == 0 && position == 1) {//导入
                    val dialog = NormalDialog(context)
                    dialog.content("您确定要切换${t.name}为当前项目数据库么?")//
                            .style(NormalDialog.STYLE_TWO)//
                            .btnNum(2)
                            .title("切换数据库")
                            .btnText("取消", "切换数据库")//
                            .show()

                    dialog.setOnBtnClickL(OnBtnClickL {
                        dialog.dismiss()
                    }, OnBtnClickL {
                        dialog.dismiss()
                        SPUtils.getInstance().put("DATA_FILENAME", t.absolutePath)
                        SPUtils.getInstance().put("DATABASE_FILENAME","bhne"+System.currentTimeMillis().toString()+".db")
                        notifyDataSetChanged()
                        ToastUtils.showShort("数据库切换成功")
                    })
                } else {
                    ToastUtils.showShort(t.absolutePath)
                }
            }
        })

    }
}